<?php
include_once("StringUtils.php");

class CouponUtils
{
	
	public static function getCode($name, $startTag)
	{
		$pos 	= strripos($name, $startTag);
		
		if($pos){
			$str 	= substr($name, $pos + strlen($startTag));
			
			$str	= trim($str);
			$str	= trim($str, '-');
			$str	= trim($str, '.');
			$str	= trim($str);
			
			$arr 	= explode(' ', $str);
			$code 	= trim($arr[0], '.');
			
			$code 	= str_ireplace('Required', '', $code);
			$code 	= str_ireplace('needed', '', $code);
			$code 	= str_ireplace('for', '', $code);
			
			$code	= trim($code, '?');
			$code	= trim($code, '!');
			$code	= trim($code, ',');
			$code	= trim($code, '"');
			$code	= trim($code, ')');
			$code	= trim($code, '(');
			$code	= trim($code, '-');
			
			
			
			if(StringUtils::isCouponCode($code)){
				if(strlen($code) < 4){
					if(StringUtils::isUpperNum($code)){
						return $code;
					}else
						return '';
				}else
					return $code;
			}
		}
		
		return '';
	}
	
	public static function findCode($name)
	{
		$arrTags = array();
		$arrTags[]	= 'code ';
		$arrTags[]	= 'code:';
		$arrTags[]	= 'code?';
		$arrTags[]	= 'code.';
		$arrTags[]	= 'code=';
		$arrTags[]	= 'coupon :';
		$arrTags[]	= 'coupon:';
		$arrTags[]	= 'code :';
		
		foreach ($arrTags as $tag) {
			$code = CouponUtils::getCode($name, $tag);
			
			if(empty($code)){
				continue;
			}else{
				return $code;
			}
		}
		
		return '';
	}
}

?>
